#!/usr/bin/env python
# coding=utf-8

from vavava.httpclient import HttpClient
from vavava.util import reg_helper

class sina(object):
    def __init__(self,log):
        self.name = ""
        self.pwd = ""
        self.client = HttpClient()
        self.client.EnableCookieSupport()
        self.log = log

    def get_fun_list(self):
        pass

    def login(self):
        html = self.client.Get("http://weibo.com/")
        self.log.info(html.decode('utf-8'))

        post_data = {
            "entry":"weibo",
            "gateway":"1",
            "from":"",
            "savestate":"7",
            "useticket":"1",
            "vsnf":"1",
            "ssosimplelogin":"1",
            "su":"cGFzY2FsLmZvci5nZW5naW5lJTQwZ21haWwuY29t",
            "service":"miniblog",
            "servertime":"1353962661",
            "nonce":"GIX3AD",
            "pwencode":"rsa2",
            "rsakv":"1330428213",
            "sp":"a5192968ae09f8d128def0ee8da9ff0c531e3bdf7506dbdcf03c9f95728d373ce917e2f40d5af60a7145f332a20461b915ce7f23c75581318a1b28fe35c6301492eb3f651729577abff10fbbbf6677e9f2d498cd35660e2281c3cf681975f3febbdfc60d801816a2b0225859538a35030610f3f51ae7c15c40a8c4071387666e",
            "encoding":"UTF-8",
            "prelt":"205",
            "url":"http://www.weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack",
            "returntype":"META"
        }
        url = r"http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.2)"
        self.client.header_refer_ = r"http://weibo.com/"
        """
        self.client.AddHeader( {
                "Set-Cookie" : "tgc=TGT-MjYwNzc1NDgwNQ==-1353966493-tc-67D05DE0500467C456DCFEACBFF91230; domain=login.sina.com.cn; path=/; Httponly ;SUS=SID-2607754805-1353966493-XD-nz75h-fc9943b812b0c357cbfd57124c33d0ef; path=/; domain=.sina.com.cn; httponly ;SUE=es=d5efbf99582771a701f5a0c90d4cc093&ev=v1&es2=cd4d401268144f65b66e5e8a8f5e6f14&rs0=KVdbzm4FeyMTN/jworiH7Dt4wRq8K8iRToe4C3KIrQulj9+QcSLhjjwrteMVELtEC8il67Z7HhsYuFVuQgNl4hTgADi3FEXpIXGN6toIvgnK0GYiDCqBs5RnAMo0Tv+HYNk2KYKiRJFHHzzc2M1y4R55fo/o+Es55mKBs21gnAA=&rv=0;path=/;domain=.sina.com.cn;Httponly ;SUP=cv=1&bt=1353966493&et=1354052893&d=40c3&i=d0ef&us=1&vf=0&vt=0&ac=2&lt=1&uid=2607754805&user=pascal.for.gengine@gmail.com&ag=4&name=pascal.for.gengine@gmail.com&nick=13818-6&sex=1&ps=0&email=&dob=&ln=pascal.for.gengine@gmail.com&os=&fmp=&lcp=2012-10-30 12:15:07;path=/;domain=.sina.com.cn ;ALC=ac=2&bt=1353966493&cv=3.0&et=1354571293&uid=2607754805&vf=0&vt=0&es=289d7536639419558b85f7e1d6556db8;expires=Monday, 03-Dec-12 21:48:13 GMT;path=/;domain=login.sina.com.cn;Httponly ;ALF=1354571293;expires=Monday, 03-Dec-12 21:48:13 GMT;path=/;domain=.sina.com.cn ;SUR=uid=2607754805&user=pascal.for.gengine@gmail.com&nick=13818-6&email=&dob=&ag=4&sex=1&ssl=0;expires=Monday, 03-Dec-12 21:48:13 GMT;path=/;domain=.sina.com.cn ;LT=1353966493; path=/; domain=login.sina.com.cn"
            }
        )
        """
        html_data = self.client.Post(url,post_data)
        html = html_data.decode('gbk')
        log.info(html)
        location = reg_helper(html,"location.replace\\(\"(?P<content>.*)\"\\)")
        html_data = self.client.Get(location[0])
        html = html_data.decode('gbk')
        log.info(html)
        pass




if __name__ == '__main__':
    import vavava.util,os
    log = vavava.util.initlog("./log/"+ os.path.split(__file__)[1]+".log")

    kk = "\\u62b1\\u6b49\\uff01\\u767b\\u5f55\\u5931\\u8d25\\uff0c\\u8bf7\\u7a0d\\u5019\\u518d\\u8bd5"
    kk = kk.replace(r'\\',"\\")

    print(kk.encode("ISO-8859-1").decode('utf-8'))


    wb = sina(log)
    html = wb.login()
    log.info(html)
    html = wb.client.Get("http://weibo.com/u/2607754805?wvr=3.6&lf=reg")
    log.info(html.decode('utf-8'))










